package com.bjsxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.TradeArea;
import com.bjsxt.dto.R;
import com.bjsxt.service.TradeAreaService;
import com.bjsxt.vo.TradeAreaMarketVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @Description:
 * @ClassName: TradeAreaController
 * @Author: chenyu
 * @Date: 2025/3/19 15:02
 */
@RestController
@RequestMapping("/tradeAreas")
@Api(tags = "交易区域的数据接口")
public class TradeAreaController {
    @Autowired
    private TradeAreaService tradeAreaService;

    @GetMapping
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "交易区域名称"),
            @ApiImplicitParam(name = "status", value = "交易区域状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
    })
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<Page<TradeArea>> findByPage(
            String name, String status,
            @ApiIgnore Page<TradeArea> page

    ) {
        Page<TradeArea> coinPage = tradeAreaService.findByPage(page, name, status);
        return R.ok(coinPage);
    }

    @PatchMapping
    @ApiOperation(value = "编辑交易区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeArea" ,value = "tradeArea的json数据")
    })
    @PreAuthorize("hasAuthority('trade_area_update')")
    public R<TradeArea> update(@RequestBody @Validated  TradeArea tradeArea){
        boolean update = tradeAreaService.updateById(tradeArea);
        if(update){
            return R.ok(tradeArea) ;
        }
        return R.fail("编辑失败") ;
    }

    @PostMapping
    @ApiOperation(value = "新增交易区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeArea" ,value = "tradeArea的json数据")
    })
    @PreAuthorize("hasAuthority('trade_area_create')")
    public R<TradeArea> save(@RequestBody @Validated  TradeArea tradeArea){
        boolean save = tradeAreaService.save(tradeArea);
        if(save){
            return R.ok(tradeArea) ;
        }
        return R.fail("新增失败") ;
    }

    @PostMapping("/status")
    @ApiOperation(value = "状态修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeArea" ,value = "tradeArea的json数据")
    })
    @PreAuthorize("hasAuthority('trade_area_update')")
    public R<TradeArea> updateStatus(@RequestBody @Validated  TradeArea tradeArea){
        boolean update = tradeAreaService.updateById(tradeArea);
        if(update){
            return R.ok(tradeArea) ;
        }
        return R.fail("状态修改失败") ;
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询交易区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status" ,value = "交易区域的状态")
    })
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<List<TradeArea>> findAll(Byte status){
        List<TradeArea> tradeAreaList = tradeAreaService.findAll(status);

        return R.ok(tradeAreaList) ;
    }

    @GetMapping("/markets")
    @ApiOperation(value = "查询交易区域，以及区域下的市场")
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<List<TradeAreaMarketVo>> getTradeAreaMarkets(){
        List<TradeAreaMarketVo> tradeAreaMarketVoList = tradeAreaService.getTradeAreaMarkets();

        return R.ok(tradeAreaMarketVoList) ;
    }

    @GetMapping("/market/favorite")
    @ApiOperation(value = "用户收藏的交易市场")
    public R<List<TradeAreaMarketVo>> getUserFavoriteMarkets(){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());

        return R.ok(tradeAreaService.getUserFavoriteMarkets(userId)) ;
    }
}
